import {createRouter, createWebHashHistory} from 'vue-router'
import { Modal } from 'ant-design-vue'
import siderDatas from '../utils/siderDatas'

const routes = [
    // {
    //     path: '/',
    //     redirect: '/home',
    // },
    {
        path: '/',
        name: 'Home',
        redirect: '/work',
        component: () => import('../layouts/Home.vue'),
        beforeEnter:(to, from, next) =>{
            let userInfo = JSON.parse(window.sessionStorage.getItem("userInfo")) || null
            if (!userInfo ) {
                Modal.info({
                    title: '警告',
                    content: '用户未登录！',
                    onOk() {
                        next('/login')
                    },
                });
            }else if(Object.keys(siderDatas).length == 0){
                Modal.info({
                    title: '警告',
                    content: '暂无数据！',
                    onOk() {
                        next('/login')
                    },
                });
            }else{
                window.sessionStorage.setItem("siderDatas", JSON.stringify(siderDatas))
                next()
            }
        },
        children: [
            {
                path: "/work",
                name: "Work",
                component: () => import('../views/Work.vue')
            },
            {
                path: "/analysis",
                name: "Analysis",
                component: () => import('../views/Analysis.vue')
            },
            {
                path: "/center",
                name: "Center",
                component: () => import('../views/Center.vue')
            },
            {
                path: '/test',
                name: "Test",
                component: () => import('../views/Test.vue')
            },
            {
                path: '/typescript',
                name: "Typescript",
                component: () => import('../views/Typescript.vue')
            },
        ]
    },
    {
        path: '/login',
        name: "Login",
        component: () => import('../layouts/Login.vue')
    },
    {
        path: '/error',
        name: "Error",
        component: () => import('../components/Error.vue')
    },

];

const router = createRouter({
    history: createWebHashHistory(), //替代之前的mode，是必须的
    routes
});
export default router;